﻿@{
    Layout = null;
    var plugName = @admin.cyt369.com.Models.PlugName.Name;
    var menuId = this.Context.Request.Query.GetInteger("MenuID", 0);
    var opListId = this.Context.Request.Query.GetString("OPListID");
    var tableName = PageViewModel.PageTableID(this, plugName);
    var tableLoadSuccess = PageViewModel.PageTableID(this, plugName, postfix:"table_success");
    var tableLoaded = PageViewModel.PageFunctionName(this, "table_loaded", plugName);
    var checkRow = PageViewModel.PageFunctionName(this, "check_row", plugName);
    var checkAllRow = PageViewModel.PageFunctionName(this, "check_all_row", plugName);
}
<div style="height:100%">

    <script type="text/javascript">
        function @tableLoadSuccess (data) {
            if (data.rows.length > 0) {
                var settings = $("#@opListId").datalist("getData");
                for (var i = 0; i < settings.rows.length; i++) {
                    for (var j = 0; j < data.rows.length; j++) {
                        if (settings.rows[i].OperationID == data.rows[j].Id) {
                            $("#@tableName").treegrid("checkRow", j);
                        }
                    }
                }
            }
            $("#@tableLoaded").val("1");
        }

        function @checkRow (id, checked) {
            if ($("#@tableLoaded").val() == "1") {
                $.ajax({
                    url: "~/Systems/SaveSystemMenuOperation",
                    type: "POST",
                    data: {
                        MenuID: @menuId,
                        OperationID: id,
                        Enable: checked
                    },
                    success: function (e) {
                        if (e.Result) {
                            $("#@opListId").datalist("reload", '~/Systems/GetSystemMenuOperations/@menuId');
                        } else {
                            $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
        }

        function @checkAllRow (rows, checked) {
            if ($("#@tableLoaded").val() == "1") {
                var list = new Array();
                for (var i = 0; i < rows.length; i++) {
                    list.push({
                        MenuID: @menuId,
                        OperationID: rows[i].Id,
                        Enable: checked
                    });
                }

                $.ajax({
                    url: "~/Systems/SaveAllSystemMenuOperation",
                    method: "POST",
                    contentType: "application/json",
                    data: JSON.stringify(list),
                    success: function (e) {
                        if (e.Result) {
                            $("#@opListId").datalist("reload", '~/Systems/GetSystemMenuOperations/@menuId');
                        } else {
                            $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
        }
    </script>
    <input id="@tableLoaded" type="hidden" value="0" />
    <table id="@tableName" class="easyui-datagrid" style="width:100%;" rownumbers="true" fit="true" fitcolumns="true"
           data-options="checkOnSelect:true,selectOnCheck:true,striped:true,border:false,url: '~/Systems/GetAppOperations',method:'get',
                     onLoadSuccess:@tableLoadSuccess,
                     onCheck: function(index, row){ @checkRow (row.Id,true); },
                     onUncheck: function(index, row){ @checkRow (row.Id,false); },
                     onCheckAll: function(rows) { @checkAllRow (rows, true); },
                     onUncheckAll: function(rows) { @checkAllRow (rows, false); }">
        <thead>
            <tr>
                <th data-options="field:'_checkbox',checkbox:true" width="30"></th>
                <th data-options="field:'Code'" width="80">编码</th>
                <th data-options="field:'Name'" width="180">名称</th>
                <th data-options="field:'Description'" width="180">说明</th>
            </tr>
        </thead>
    </table>
</div>